package com.digital.herbarium.Entity;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class Comment {
    private Integer id;
    private Integer pid;
    private String userid;
    private Integer anonymous;
    private Integer state;
    private String content;
    private Integer totop;
    private String toptime;
    private String title;

    public Integer getTotop() {
        return totop;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setTotop(Integer totop) {
        this.totop = totop;
    }

    public String getToptime() {
        return toptime;
    }

    public void setToptime(String toptime) {
        this.toptime = toptime;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getPid() {
        return pid;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }

    public String  getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public Integer getAnonymous() {
        return anonymous;
    }

    public void setAnonymous(Integer anonymous) {
        this.anonymous = anonymous;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getCreatetime() {
        return createtime;
    }

    public void setCreatetime(String createtime) {
        this.createtime = createtime;
    }

    public String getUsername()
    {
        if (username == null || username.isEmpty()) {
            username = generateRandomValue();
        }
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    private String createtime;
    private String username;


    // 随机生成一个值
    private String generateRandomValue() {
        String randomValue = "";
        // long timestamp = System.currentTimeMillis();
        while (true) {
            randomValue = "匿名_" + UUID.randomUUID().toString().replace("-", "").substring(0, 8);
            if (!isExist(randomValue)) {
                addRandomValue(randomValue);
                break;
            }
        }
        return randomValue;
    }

    private static List<String> randomValues = new ArrayList<>();

    private boolean isExist(String randomValue) {
        return randomValues.contains(randomValue);
    }

    private void addRandomValue(String randomValue) {
        randomValues.add(randomValue);
    }
}
